package com.smile.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.smile.api.FlyApi;
import com.smile.api.OperateLogApi;
import com.smile.api.PayloadApi;
import com.smile.mapper.*;
import com.smile.model.domain.*;
import com.smile.model.domain.FeisuanInspectionTask;
import com.smile.model.request.CameraFocalLengthSetRequest;

import com.smile.model.response.response.FlightDataResponseDTO;
import com.smile.model.response.response.PayloadDataResponseDTO;
import com.smile.service.CommonService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.concurrent.locks.ReentrantLock;

@Service
@Slf4j
public class CommonServiceImpl implements CommonService {


    @Autowired
    private AirportMapper airportMapper;

    @Autowired
    private AirportInfoMapper  airportInfoMapper;
    @Autowired
    private OperateLogApi operateLogApi;
    @Autowired
    private DroneMapper droneMapper;


    @Autowired
    private FlightTaskMapper flightTaskMapper;

    @Autowired
    private InspectionTaskMapper inspectionTaskMapper;

    @Autowired
    private DronePayloadMapper dronePayloadMapper;

    @Autowired
    private AirportDroneMapper airportDroneMapper;

    @Autowired
    private PayloadApi payloadApi;

    private ReentrantLock reentrantLock = new ReentrantLock();
    @Autowired
    FlyApi flyApi;


    @Override
    public PayloadDataResponseDTO getAirportData(String gatewaySn) throws IOException{
        String payloadIndex = null;
        PayloadDataResponseDTO  payloadDataResponseDTO = new  PayloadDataResponseDTO();
        QueryWrapper<FeisuanAirport>  queryWrapper3 = new QueryWrapper<>();
        queryWrapper3.eq("gateway_sn",gatewaySn);
        queryWrapper3.last("limit 1");
        FeisuanAirport feisuanAirport = airportMapper.selectOne(queryWrapper3);

        if(!ObjectUtil.isNull(feisuanAirport)){
            QueryWrapper<FeisuanAirportDrone> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("airport_id",feisuanAirport.getId());
            FeisuanAirportDrone feisuanAirportDrone  = airportDroneMapper.selectOne(queryWrapper);
            if(ObjectUtil.isNotNull(feisuanAirportDrone)){
                FeisuanDrone feisuanDrone = droneMapper.selectById(feisuanAirportDrone.getDroneId());
                if(ObjectUtil.isNotNull(feisuanDrone)){
                    QueryWrapper<FeisuanDronePayload> queryWrapper1 = new QueryWrapper<>();
                    queryWrapper1.eq("drone_sn",feisuanDrone.getDroneSn());
                    FeisuanDronePayload feisuanDronePayload = dronePayloadMapper.selectOne(queryWrapper1);
                    if(ObjectUtil.isNotNull(feisuanDronePayload)){
                        CameraFocalLengthSetRequest cameraFocalLengthSetRequest = new CameraFocalLengthSetRequest();
                        try {
                            payloadIndex = feisuanDronePayload.getPayloadIndex();
                            payloadDataResponseDTO.setSn(feisuanDrone.getDroneSn());
                            payloadDataResponseDTO.setPayloadIndex(payloadIndex);
                            payloadDataResponseDTO.setGateway(feisuanAirport.getGatewaySn());
                        }catch (Exception e){

                        }
                    }
                }
            }
        }
        return   payloadDataResponseDTO;
    }


    @Override
    public FlightDataResponseDTO getFlightData(String FlightId) throws IOException{
        FlightDataResponseDTO flightDataResponseDTO = new  FlightDataResponseDTO();
        flightDataResponseDTO.setTaskType(1);
        //查询飞行记录是否存在
        QueryWrapper<FeisuanFlightTask> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("flight_id",FlightId);
        FeisuanFlightTask feisuanFlightTask = flightTaskMapper.selectOne(queryWrapper);
        String gatewaySn = null;
        Integer flightType = null;
        if(ObjectUtil.isNotNull(feisuanFlightTask)){
            gatewaySn = feisuanFlightTask.getGatewaySn();
            flightType = feisuanFlightTask.getTaskType();
        }
        FeisuanAirport feisuanAirport = new  FeisuanAirport();
        if (gatewaySn == null) {
            QueryWrapper<FeisuanInspectionTask> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("flight_id",FlightId);
            FeisuanInspectionTask feisuanInspectionTask = inspectionTaskMapper.selectOne(queryWrapper1);
            if(ObjectUtil.isNotNull(feisuanInspectionTask)){
                flightDataResponseDTO.setAirportId(feisuanInspectionTask.getAirportId());
                flightDataResponseDTO.setAirlineId(feisuanInspectionTask.getAirlineId());
                flightDataResponseDTO.setOrganizationId(feisuanInspectionTask.getOrganizationId());
                flightDataResponseDTO.setFlightId(feisuanInspectionTask.getFlightId());
                flightDataResponseDTO.setPlanName(feisuanInspectionTask.getPlanName());
                feisuanAirport = airportMapper.selectById(feisuanInspectionTask.getAirportId());
            }else{
                return flightDataResponseDTO;
            }
        }else if(flightType != null){
            if(flightType == 2){
                flightDataResponseDTO.setTaskType(2);
            }else{
                QueryWrapper<FeisuanInspectionTask> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq("flight_id",FlightId);
                FeisuanInspectionTask feisuanInspectionTask = inspectionTaskMapper.selectOne(queryWrapper1);
                if(ObjectUtil.isNotNull(feisuanInspectionTask)) {
                    flightDataResponseDTO.setAirportId(feisuanInspectionTask.getAirportId());
                    flightDataResponseDTO.setAirlineId(feisuanInspectionTask.getAirlineId());
                    flightDataResponseDTO.setOrganizationId(feisuanInspectionTask.getOrganizationId());
                    flightDataResponseDTO.setFlightId(feisuanInspectionTask.getFlightId());
                    flightDataResponseDTO.setPlanName(feisuanInspectionTask.getPlanName());
                }
            }
            QueryWrapper<FeisuanAirport> queryWrapper3 = new QueryWrapper<>();
            queryWrapper3.eq("gateway_sn",gatewaySn);
            queryWrapper3.last("limit 1");
            feisuanAirport = airportMapper.selectOne(queryWrapper3);
        }



        if(!ObjectUtil.isNull(feisuanAirport)) {
            if (ObjectUtil.isNull(flightDataResponseDTO.getOrganizationId())) {
                flightDataResponseDTO.setOrganizationId(feisuanAirport.getOrganizationId());
            }
            flightDataResponseDTO.setAirportId(feisuanAirport.getId());
            flightDataResponseDTO.setGateway(feisuanAirport.getGatewaySn());
            QueryWrapper<FeisuanAirportDrone> queryWrapperD = new QueryWrapper<>();
            queryWrapper.eq("airport_id", feisuanAirport.getId());
            queryWrapperD.last("limit 1");

            FeisuanAirportDrone feisuanAirportDrone = airportDroneMapper.selectOne(queryWrapperD);

            if (ObjectUtil.isNotNull(feisuanAirportDrone)) {
                FeisuanDrone feisuanDrone = droneMapper.selectById(feisuanAirportDrone.getDroneId());
                if (ObjectUtil.isNotNull(feisuanDrone)) {
                    QueryWrapper<FeisuanDronePayload> queryWrapper1 = new QueryWrapper<>();
                    queryWrapper1.eq("drone_sn", feisuanDrone.getDroneSn());
                    flightDataResponseDTO.setDroneId(feisuanAirportDrone.getDroneId());
                    flightDataResponseDTO.setSn(feisuanDrone.getDroneSn());
//                    flightDataResponseDTO.setDroneId(feisuanDrone.getId());
                    FeisuanDronePayload feisuanDronePayload = dronePayloadMapper.selectOne(queryWrapper1);
                    if (ObjectUtil.isNotNull(feisuanDronePayload)) {
                        CameraFocalLengthSetRequest cameraFocalLengthSetRequest = new CameraFocalLengthSetRequest();
                        try {
                            flightDataResponseDTO.setPayloadIndex(feisuanDronePayload.getPayloadIndex());
//                            payloadIndex = feisuanDronePayload.getPayloadIndex();
//                            payloadDataResponseDTO.setSn(feisuanDrone.getDroneSn());
//                            payloadDataResponseDTO.setPayloadIndex(payloadIndex);
//                            payloadDataResponseDTO.setGateway(feisuanAirport.getGatewaySn());
                        } catch (Exception e) {

                        }
                    }
                }
            }
        }
        return  flightDataResponseDTO;
    }
}
